import {observable, action, runInAction} from 'mobx';
import {Toast} from 'antd-mobile';

/**
 * mobx 状态管理类，所有的逻辑处理都在这里
 * */
class CarListStore {
    @observable carData = []; //业主列表
    @observable parkInfo;
    @observable visible;
    @observable value = ['粤', 'B', '', '', '', '', '', '']; //输入车牌值
    @observable index;
    @observable inputType;
    @observable disabled = true;
    @observable carouselIndex = 0;
    pageSize = 10;

    @action
    closeSelect = () => {
        runInAction(() => {
            this.index = null;
            this.visible = false;
        });
    };

    @action
    fallback = () => {
        runInAction(() => {
            if (this.index > 0) {
                this.value[this.index] = '';
                this.index = --this.index;
            }
        });
    };

    @action
    selectInput = (index) => {
        runInAction(() => {
            this.index = index;
            this.visible = true;
        });
    };

    @action
    selectCallback = (ver) => {
        runInAction(() => {
            this.value[this.index] = ver;
            if (this.index < 7) {
                this.index = ++this.index;
            }
        });
    };

    getVehicleNumber() {
        let vehicleNumber = '';
        let values = this.value.slice();
        let isOk = false;
        for (let i in values) {
            vehicleNumber += values[i];
        }
        if (vehicleNumber.length === 8) {
            isOk = true;
        } else {
            isOk = (vehicleNumber.length === 7 && !this.value[7]);
        }

        if (!isOk) {
            Toast.info('车牌号错误!!!', 1);
            throw '车牌号错误!!!';
        }

        return vehicleNumber;
    }

    @action
    carouselChange = (value) => {
        if (value > -1 && this.carData && this.carData.length > value) {
            this.carouselIndex = value;
        }
    };

    @action
    loadData = () => {
        let params = {};
        params.commCode = commCode;
        postJson("/api/car/anon/getParkInfo", params, (data) => {
            this.parkInfo = data[0];
            console.log("----->  ", this.parkInfo);
            params = {};
            params.parkId = this.parkInfo.parkId;
            params.ownerCode = ownerCode;
            postJson("/car/carInfo/getCarInfoList.x", params, (data) => {
                runInAction(() => {
                    console.log("data", data);
                    this.carData = data;
                    this.refreshing = false;
                    this.isLoading = false;
                });
            }, (error) => {
                runInAction(() => {
                    this.refreshing = false;
                    this.isLoading = false;
                });
            });
        });
    };

    @action
    selectCar = (carInfo) => {
        runInAction(() => {
            this.value = ['粤', 'B', '', '', '', '', '', ''];
            for (let i in carInfo.plateNumber) {
                this.value[i] = carInfo.plateNumber[i];
            }
        });
    };

    carPay = (props) => {
        if (this.parkInfo) {
            this.parkInfo.plateNumber = this.getVehicleNumber();
            props.carOrderStore.setOrderInfo(this.parkInfo);
            props.history.push('/pages/mobile/car/carOrder');
        } else {
            Toast.info('您所在小区暂未开通停车场服务!', 1);
        }
    };
}

export default new CarListStore()